package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.Category;
import com.spzx.product.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("category")
@Tag(name = "分类管理模块")
public class CategoryController extends BaseController {
    @Resource
    CategoryService categoryService;
    @Operation(summary = "导入分类")
    @PostMapping("import") //前端上传excel文件，后端解析，每一行转为一个分类存到分类表中
    public AjaxResult importCates(@RequestParam MultipartFile file){
        return toAjax(categoryService.importCates(file));
    }
    @Operation(summary = "导出所有分类")
    @PostMapping("export")
    //导出就是文件下载： 查询数据库分类列表，写入到excel文件 给客户端
    public void export(HttpServletResponse response){
        categoryService.exportCates(response);
    }
    @Operation(summary = "根据分类id查询下一级分类")
    @GetMapping("listByPid/{pid}")
    public AjaxResult listByPid(@PathVariable Long pid){
        List<Category> categoryList = categoryService.listByPid(pid);
        return success(categoryList);
    }
    @InnerAuth
    @Operation(summary = "内部调用接口：获取首页分类集合")
    @GetMapping("listIndexCates")
    public R<List<Category>> listIndexCates(){
        List<Category> categoryList = categoryService.listByPid(0L);
        return R.ok(categoryList);
    }

    @InnerAuth
    @Operation(summary = "内部调用接口：获取分类页分类嵌套集合")
    @GetMapping("listCateTree")
    public R<List<Category>> listCateTree(){
        List<Category> categoryList = categoryService.listCateTree();
        return R.ok(categoryList);
    }
}
